###############################################################################
############################01. Preparing CHES Data############################
###############################################################################

#Read in Data#

library(readr)

CHES <- read_csv("CHES_means_2017.csv")

library(readxl)

CHES_full <- read_excel("1999-2014_CHES_dataset_means.xlsx")

#Make Data Conformable for Adding 2017 to 1999-2014 trend file#

library(dplyr)

as_tibble(CHES)

#Adjust experts to expert#

CHES <- rename(CHES, expert = experts) 

#Split family (Character) into family_no (numeric) and family (character)#

CHES <- tidyr::separate(CHES, family, c("family_no", "family")) 

#Adjust variables in nonconformable classes#

CHES$govt <- as.character(CHES$govt) 

#Convert text to numbers for position variables#

CHES_full$lrgen[CHES_full$lrgen == "extreme left"] <- 0
CHES_full$lrgen[CHES_full$lrgen == "extreme right"] <- 10
CHES_full$lrgen[CHES_full$lrgen == "center"] <- 5
CHES_full$lrgen <- as.numeric(CHES_full$lrgen)

CHES_full$lrecon[CHES_full$lrecon == "extreme left"] <- 0
CHES_full$lrecon[CHES_full$lrecon == "extreme right"] <- 10
CHES_full$lrecon[CHES_full$lrecon == "center"] <- 5
CHES_full$lrecon <- as.numeric(CHES_full$lrecon)

CHES_full$galtan[CHES_full$galtan == "extreme gal"] <- 0
CHES_full$galtan[CHES_full$galtan == "extreme tan"] <- 10
CHES_full$galtan[CHES_full$galtan == "center"] <- 5
CHES_full$galtan <- as.numeric(CHES_full$galtan)

CHES_full$position[CHES_full$position == "strongly opposed"] <- 1
CHES_full$position[CHES_full$position == "opposed"] <- 2
CHES_full$position[CHES_full$position == "somewhat opposed"] <- 3
CHES_full$position[CHES_full$position == "neutral"] <- 4
CHES_full$position[CHES_full$position == "someqhat in favor"] <- 5
CHES_full$position[CHES_full$position == "in favor"] <- 6
CHES_full$position[CHES_full$position == "strongly in favor"] <- 7
CHES_full$position <- as.numeric(CHES_full$position)

CHES_full$eu_asylum <- NULL

#Adds 2017 data to 1999-2014 data#

CHES <- bind_rows(CHES_full, CHES) 

#Fixes naming problems

CHES$country[CHES$country == "ger"] <- "ge" 
CHES$country[CHES$country == "swe"] <- "sv"

#Arranges data by country, party, and year#

CHES <- CHES %>%
  arrange(country, electionyear, party_id) 

# Adds numeric country identifier - ALPHABETIC NOT SAME AS IN CHES#

CHES <- CHES %>% 
  mutate(country_id = case_when(
    country == "aus" ~ 1,
    country == "be" ~ 2,
    country == "bul" ~ 3,
    country == "cro" ~ 4,
    country == "cyp" ~ 5,
    country == "cz" ~ 6,
    country == "dk" ~ 7,
    country == "esp" ~ 8,
    country == "est" ~ 9,
    country == "fin" ~ 10,
    country == "fr" ~ 11,
    country == "ge" ~ 12,
    country == "gr" ~ 13,
    country == "hun" ~ 14,
    country == "irl" ~ 15,
    country == "it" ~ 16,
    country == "lat" ~ 17,
    country == "lith" ~ 18,
    country == "lux" ~ 19,
    country == "mal" ~ 20,
    country == "nl" ~ 21,
    country == "pol" ~ 22,
    country == "por" ~ 23,
    country == "rom" ~ 24,
    country == "sle" ~ 25,
    country == "slo" ~ 26,
    country == "sv" ~ 27,
    country == "uk" ~ 28
  ))

#Country as factor

CHES$country <- as.factor(CHES$country)

#Adds east/west designation for 2017 entries

CHES <- CHES %>% 
  mutate(eastwest = case_when(
    country == "aus" | country == "be" | country == "dk" | country == "esp" | country == "fin" | country == "fr" | country == "ge" | country == "gr" | country == "irl" | country == "it" | country == "lux" | country == "nl" | country == "por" | country == "sv" | country == "uk" ~ "west",
    country == "bul" | country == "cro" | country == "cyp" | country == "cz" | country == "est" | country == "hun" | country == "lat" | country == "lith" | country == "mal" | country == "pol" | country == "rom" | country == "sle" | country == "slo" ~ "east"
  ))

#Adds membership status (all cases in 2017 flash survey were EU members)

CHES$eumember[CHES$year == 2017] <- "EU member state" 

#as factor variable

CHES$eumember <- as.factor(CHES$eumember) 

#Numeric variant

CHES$eumembernum[CHES$eumember == "other"] <- 0
CHES$eumembernum[CHES$eumember == "EU member state"] <- 1

#Make party family a factor and correct inconsistencies with 2017 data

CHES$family <- as.factor(CHES$family)

CHES$family[CHES$family == "no"] <- "no family"

CHES$family[(CHES$party_id == 2103 | CHES$party_id == 504 | CHES$party_id == 525 |CHES$party_id == 627 | CHES$party_id == 601 | CHES$party_id == 306 | CHES$party_id == 404 | CHES$party_id == 403| CHES$party_id == 1014 | CHES$party_id == 1208| CHES$party_id == 1201 | CHES$party_id == 1601) & CHES$year == 2017] <- "rad left"

CHES$family[CHES$family == "radical"] <- "rad right"

CHES$family[CHES$family == "Christian"] <- "christdem"

CHES$family[CHES$family == "conservative"] <- "cons"

CHES$family[CHES$family == "agrarian"] <- "agrarian/centre"

#Make government a factor and correct inconsistencies

CHES$govt <- as.factor(CHES$govt)

CHES$govt[CHES$govt == "0"] <- "not in government"

CHES$govt[CHES$govt == "1"] <- "in government"

#Numeric government variant

CHES$govtnum <- NA 

CHES$govtnum[CHES$govt == "in government"] <- 1

CHES$govtnum[CHES$govt == "not in government"] <- 0

CHES$govtnum[CHES$govt == "0.5"] <- .5

#mips as factors

CHES$mip_one <- as.factor(CHES$mip_one)

CHES$mip_two <- as.factor(CHES$mip_two)

CHES$mip_three <- as.factor(CHES$mip_three)

#corrections for belgium 2002 wave to match to election year 2003

CHES$electionyear[CHES$year == 2002 & CHES$country == 'be'] <- 2003

CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "PS"] <- 11.4
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "SP"] <- 14.9
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "ECOLO"] <- 3.1
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "AGALEV"] <- 2.5
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "MR"] <- 11.4
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "VLD"] <- 15.4
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "CDH"] <- 5.5
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "CD&V"] <- 13.3
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "NVA"] <- 3.1
CHES$vote[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "VB"] <- 11.6

CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "PS"] <- 16.7
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "SP"] <- 15.3
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "ECOLO"] <- 2.7
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "AGALEV"] <- 0
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "MR"] <- 16
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "VLD"] <- 16.7
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "CDH"] <- 5.3
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "CD&V"] <- 14
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "NVA"] <- .7
CHES$seat[CHES$electionyear == 2003 & CHES$country == 'be' & CHES$party == "VB"] <- 12

#Corrections for Belgium 2006 wave to match to election year 2007#

CHES$electionyear[CHES$year == 2006 & CHES$country == 'be'] <- 2007

CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "PS"] <- 10.9
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "SPA"] <- 10.3
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "ECOLO"] <- 5.1
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "GROEN"] <- 4
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "MR"] <- 12.5
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "VLD"] <- 11.8
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "CDH"] <- 6.1
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "CD&V"] <- 16
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "NVA"] <- 4
CHES$vote[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "VB"] <- 12

CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "PS"] <- 13.3
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "SPA"] <- 9.3
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "ECOLO"] <- 5.3
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "GROEN"] <- 2.7
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "MR"] <- 15.3
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "VLD"] <- 12
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "CDH"] <- 6.7
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "CD&V"] <- 25/150*100
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "NVA"] <- 5/150*100
CHES$seat[CHES$electionyear == 2007 & CHES$country == 'be' & CHES$party == "VB"] <- 11.3

#Correction for UK 2014 wave to 2015 electionyear#

CHES$electionyear[CHES$year == 2014 & CHES$country == 'uk'] <- 2015

CHES$vote[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "CONS"] <- 36.9
CHES$vote[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "LAB"] <- 30.5
CHES$vote[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "LibDem"] <- 7.9
CHES$vote[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "SNP"] <- 4.7
CHES$vote[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "PLAID"] <- .6
CHES$vote[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "GREEN"] <- 3.8
CHES$vote[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "UKIP"] <- 12.7

CHES$seat[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "CONS"] <- 50.9
CHES$seat[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "LAB"] <- 35.7
CHES$seat[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "LibDem"] <- 1.2
CHES$seat[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "SNP"] <- 8.6
CHES$seat[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "PLAID"] <- .5
CHES$seat[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "GREEN"] <- .2
CHES$seat[CHES$electionyear == 2015 & CHES$country == 'uk' & CHES$party == "UKIP"] <- .2

#Correctin Italy wave 2017 to electionyear 2018

CHES$electionyear[CHES$year == 2017 & CHES$country == 'it'] <- 2018

CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "LN"] <- 17.4
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "UDC"] <- 1.3
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "FI"] <- 14
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "SVP"] <- .4
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "PD"] <- 18.8
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "CD"] <- 0
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "FdI"] <- 4.4
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "M5S"] <- 33.3
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "VdA"] <- NA
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "SI"] <- NA
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "MDP"] <- NA
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "AP"] <- NA

CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "LN"] <- 19.7
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "UDC"] <- .6
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "FI"] <- 16.8
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "SVP"] <- .6
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "PD"] <- 17.8
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "CD"] <- .2
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "FdI"] <- 4.9
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "M5S"] <- 36
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "VdA"] <- NA
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "SI"] <- NA
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "MDP"] <- NA
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'it' & CHES$party == "AP"] <- NA

#Correction for Sweden wave 2017 to electionyear 2018#

CHES$electionyear[CHES$year == 2017 & CHES$country == 'sv'] <- 2018

CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "V"] <- 8
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "SAP"] <- 28.3
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "C"] <- 8.6
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "FP"] <- 5.5
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "M"] <- 19.8
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "KD"] <- 6.3
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "MP"] <- 4.4
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "SD"] <- 17.5
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "PIRAT"] <- NA
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "FI"] <- NA

CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "V"] <- 8
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "SAP"] <- 28.7
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "C"] <- 8.9
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "FP"] <- 5.7
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "M"] <- 20.1
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "KD"] <- 6.3
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "MP"] <- 4.6
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "SD"] <- 17.8
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "PIRAT"] <- NA
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'sv' & CHES$party == "FI"] <- NA

#Correction for Hungary 2017 wave to electionyear 2018

CHES$electionyear[CHES$year == 2017 & CHES$country == 'hun'] <- 2018

CHES$vote[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "MSzP"] <- 11.9
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "Fidesz"] <- 43.37
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "JOBBIK"] <- 19.1
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "LMP"] <- 7.1
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "E14"] <- NA
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "DK"] <- 5.4
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "PM"] <- NA
CHES$vote[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "MLP"] <- NA

CHES$seat[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "MSzP"] <- 10.1
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "Fidesz"] <- 58.8
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "JOBBIK"] <- 13.1
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "LMP"] <- 4.0
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "E14"] <- NA
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "DK"] <- 4.5
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "PM"] <- NA
CHES$seat[CHES$electionyear == 2018 & CHES$country == 'hun' & CHES$party == "MLP"] <- NA


CHES <- CHES %>%
  filter(!(CHES$country == "fin" & CHES$year == 2002),!(CHES$country == "fr" & CHES$year == 2006),!(CHES$country == "hun" & CHES$year == 2017),!(CHES$country == "irl" & CHES$year == 2006),!(CHES$country == "sv" & CHES$year == 2017))

#Creates Unique identifier for each country-electionyear

CHES <- CHES %>% 
  mutate(countryelectionyear = group_indices_(CHES, .dots = c("country", "electionyear")))

####Calculate Polarization Indices####

##Total Span of Dimension (unweighted)##

#Total span of Economic dimension

econspan <- NA 
cases <- seq(1:124)

for(i in CHES$countryelectionyear){
  econspan[i] <- max(CHES$lrecon[CHES$countryelectionyear == i], na.rm = T) - min(CHES$lrecon[CHES$countryelectionyear == i], na.rm = T) }

#Total span of GALTAN dimension

galtanspan <-NA 

for(i in CHES$countryelectionyear){
  galtanspan[i] <- max(CHES$galtan[CHES$countryelectionyear == i], na.rm = T) - min(CHES$galtan[CHES$countryelectionyear == i], na.rm = T) 
}

##Standard deviation weighted by vote share##

library(SDMTools)

#Weighted Standard deviation of Economic Dimension

econsdw <- NA 

for(i in CHES$countryelectionyear){
  econsdw[i] <- wt.sd(CHES$lrecon[CHES$countryelectionyear == i], CHES$vote[CHES$countryelectionyear == i])
}

#Weighted Standard deviation of GALTAN Dimension

galtansdw <- NA 

for(i in CHES$countryelectionyear){
  galtansdw[i] <- wt.sd(CHES$galtan[CHES$countryelectionyear == i], CHES$vote[CHES$countryelectionyear == i])
}

#Weighted STandard Deviation of spendvtax#

spendsdw <- NA 

for(i in CHES$countryelectionyear){
  spendsdw[i] <- wt.sd(CHES$spendvtax[CHES$countryelectionyear == i], CHES$vote[CHES$countryelectionyear == i])
}

#Weighted STandard Deviation of redistribution#

redistsdw <- NA 

for(i in CHES$countryelectionyear){
  redistsdw[i] <- wt.sd(CHES$redistribution[CHES$countryelectionyear == i], CHES$vote[CHES$countryelectionyear == i])
}



##Dalton PI with weighted mean instead of unweighted mean##

#PI of economic l-r

econpiw <- NA 

econw <- CHES %>% group_by(countryelectionyear) %>% 
  tidyr::replace_na(list(vote = 0)) %>%
  mutate(econ_avgw = weighted.mean(lrecon, vote, na.rm = T)) %>%
  summarize(sqrt(sum(vote * ((lrecon - econ_avgw) / 5)^2))) # replaces NAs w/ 0s
econpiw <- econw[,2]

#PI of galtan

galtanpiw <- NA 
galw <- CHES %>% group_by(countryelectionyear) %>%
  tidyr::replace_na(list(vote = 0)) %>%
  mutate(galtan_avgw = weighted.mean(galtan, vote)) %>%
  summarize(sqrt(sum(vote * ((galtan - galtan_avgw) / 5)^2)))

galtanpiw <- galw[,2]

#PI of spendvtax

spendpiw <- NA 
spendw <- CHES %>% group_by(countryelectionyear) %>%
  tidyr::replace_na(list(vote = 0)) %>%
  mutate(spendvtax_avgw = weighted.mean(spendvtax, vote)) %>%
  summarize(sqrt(sum(vote * ((spendvtax - spendvtax_avgw) / 5)^2)))

spendpiw <- spendw[,2]

#PI of redistribution

redistpiw <- NA 
redistw <- CHES %>% group_by(countryelectionyear) %>%
  tidyr::replace_na(list(vote = 0)) %>%
  mutate(redist_avgw = weighted.mean(redistribution, vote)) %>%
  summarize(sqrt(sum(vote * ((redistribution - redist_avgw) / 5)^2)))

redistpiw <- redistw[,2]

#spendvtax salience

spendsal <- NA

spend <- CHES %>%
  group_by(countryelectionyear) %>%
  tidyr::replace_na(list(vote = 0)) %>%
  summarize(spendsal = mean(spendvtax_salience, na.rm = T))

spendsal <- spend[,2]

#redistribution salience

redistsal <- NA

red <- CHES %>%
  group_by(countryelectionyear) %>%
  tidyr::replace_na(list(vote = 0)) %>%
  summarize(redsal = mean(redist_salience, na.rm = T))

redistsal <- red[,2]

#percent green or rad right#

percgt <- NA

CHES$grrad <- ifelse(CHES$family == 'green' | CHES$family == 'rad right',
                     CHES$vote,
                     NA)

perc <- CHES %>%
  group_by(countryelectionyear) %>%
  tidyr::replace_na(list(vote = 0)) %>%
  summarize(percgt = sum(grrad, na.rm = T))

percgt <- perc[,2]


####Combine into Dataset####

#Combine polarization indexes into a single data set

pols <- data.frame(cbind(econspan, galtanspan,
                         econsdw, galtansdw,
                         econpiw, galtanpiw,
                         redistsdw, spendsdw,
                         redistsal,spendsal, percgt))

#Fix  polarization index names

colnames(pols)[5] <- "econpiw"
colnames(pols)[6] <- "galtanpiw"


#collapse CHES into truncated, which has only one row per country election year#

ches <- CHES %>%
  group_by(countryelectionyear)%>%
  select(country, country_id, year, electionyear, countryelectionyear, eastwest)

truncated<- distinct(ches, countryelectionyear, .keep_all = TRUE)

#move over identifier variables

pols$country <- truncated$country
pols$country_id <- truncated$country_id
pols$electionyear <- truncated$electionyear
pols$countryelectionyear <- truncated$countryelectionyear
pols$eastwest <- truncated$eastwest
pols$year <- truncated$year

##Matching Country Names#

pols <- pols %>% mutate(country.name = case_when(
  pols$country == "aus" ~ "Austria",
  pols$country == "be" ~ "Belgium",
  pols$country == "bul" ~ "Bulgaria",
  pols$country == "cro" ~ "Croatia",
  pols$country == "cyp" ~ "Cyprus",
  pols$country == "cz" ~ "Czech Republic",
  pols$country == "dk" ~ "Denmark",
  pols$country == "esp" ~ "Spain",
  pols$country == "est" ~ "Estonia",
  pols$country == "fin" ~ "Finland",
  pols$country == "fr" ~ "France",
  pols$country == "ge" ~ "Germany",
  pols$country == "gr" ~ "Greece",
  pols$country == "hun" ~ "Hungary",
  pols$country == "irl" ~ "Ireland",
  pols$country == "it" ~ "Italy",
  pols$country == "lat" ~ "Latvia",
  pols$country == "lith" ~ "Lithuania",
  pols$country == "lux" ~ "Luxembourg",
  pols$country == "mal" ~ "Malta",
  pols$country == "nl" ~ "Netherlands",
  pols$country == "pol" ~ "Poland",
  pols$country == "por" ~ "Portugal",
  pols$country == "rom" ~ "Romania",
  pols$country == "sle" ~ "Slovenia",
  pols$country == "slo" ~ "Slovakia",
  pols$country == "sv" ~ "Sweden",
  pols$country == "uk" ~ "Great Britain"
))

